package www.bd.com.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import www.bd.com.dao.LabelDao;
import www.bd.com.pojo.Label;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Berret
 * @date 2019/4/11 16:38
 * @discription :
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部标签
     * @return
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }
    /**
     * 根据ID查询标签
     * @return
     */
    public Label findById(String id){
        return labelDao.findById(id).get();
    }
    /**
     * 增加标签
     * @param label
     */
    public void add(Label label){
        label.setId( idWorker.nextId()+"" );//设置ID
        labelDao.save(label);
    }
    /**
     * 修改标签
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }
    /**
     * 删除标签
     * @param id
     */
    public void deleteById(String id){
        labelDao.deleteById(id);
    }

    public List<Label> findSearch(Label label) {
        return labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param root 根对象 查询的条件封装的对象
             * @param query 封装查询的关键字 group by ,order by
             * @param cb 封装查询的条件对象
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                ArrayList<Predicate> predicateList = new ArrayList<>();

                if(label.getLabelname()!=null && !"".equals(label.getLabelname())){
                    Predicate labelname = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    predicateList.add(labelname);
                }
                if(label.getState()!=null && !"".equals(label.getState())){
                    Predicate state = cb.equal(root.get("state").as(String.class), label.getState());
                    predicateList.add(state);
                }

                Predicate[] array = new Predicate[predicateList.size()];
                predicateList.toArray(array);

                return cb.and(array);
            }
        });
    }

    public Page<Label> pageQuery(int page,int size,Label label) {
        Pageable pageable = PageRequest.of(page-1,size);
        return labelDao.findAll(new Specification<Label>() {
            /**
             *
             * @param root 根对象 查询的条件封装的对象
             * @param query 封装查询的关键字 group by ,order by
             * @param cb 封装查询的条件对象
             * @return
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                ArrayList<Predicate> predicateList = new ArrayList<>();

                if(label.getLabelname()!=null && !"".equals(label.getLabelname())){
                    Predicate labelname = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    predicateList.add(labelname);
                }
                if(label.getState()!=null && !"".equals(label.getState())){
                    Predicate state = cb.equal(root.get("state").as(String.class), label.getState());
                    predicateList.add(state);
                }

                Predicate[] array = new Predicate[predicateList.size()];
                predicateList.toArray(array);

                return cb.and(array);
            }
        },pageable);
    }
}
